package com.xuequ.answerme.store;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xuequ.answerme.R;
import com.xuequ.answerme.adapter.FocusRecyclerViewAdapter;
import com.xuequ.answerme.adapter.LikeRecyclerViewAdapter;
import com.xuequ.answerme.beans.AnswerBean;
import com.xuequ.answerme.beans.UserBean;
import com.xuequ.answerme.manager.QuestionManager;
import com.xuequ.answerme.manager.UserManager;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobUser;

/**
 * Created by Shijie on 2015/9/28.
 */
public class LikeViewPagerFragment extends Fragment {

    private RecyclerView mRecyclerView;
    private LikeRecyclerViewAdapter mLikeAdapter;
    private List<AnswerBean> mData;

    private boolean isRefreshing = false;

    private QuestionManager questionManager;
    private UserManager userManager;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.store_like_layout, container, false);
        initViews(view);
        firstRefresh();

        return view;
    }

    private void initViews(View view) {

        //new something
        if (questionManager == null) {
            questionManager = new QuestionManager();
        }
        if (userManager == null) {
            userManager = new UserManager();
        }

        mRecyclerView = (RecyclerView) view.findViewById(R.id.store_like_recyclerView);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setHasFixedSize(true);

        mData = new ArrayList<>();
        mLikeAdapter = new LikeRecyclerViewAdapter(mData);
        mRecyclerView.setAdapter(mLikeAdapter);

        mLikeAdapter.setOnRefreshDoneListener(new LikeRecyclerViewAdapter.OnRefreshDoneListener() {
            @Override
            public void onRefreshDone() {
                isRefreshing = false;
            }
        });
        mLikeAdapter.setOnItemClickListener(new LikeRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

            }
        });

    }

    private void firstRefresh() {
        if (!isRefreshing) {
            isRefreshing = true;
            userManager.findLikes(getActivity(), BmobUser.getCurrentUser(getActivity(), UserBean.class), new UserManager.FindLikeListener() {
                @Override
                public void findLikeOnSuccess(List<AnswerBean> answerBeans, int count) {
                    if (answerBeans.size() != 0) {
                        mLikeAdapter.refreshData(answerBeans);
                    }else {
                        isRefreshing = true;
                    }
                }

                @Override
                public void findLikeOnFailure(String failureMsg) {
                    isRefreshing = false;
                }
            });
        } else {
            Log.i("LikeViewPagerFragment", "is refreshing");
        }
    }

    public void callToRefreshData(){

    }


}
